package parsers;

import java.util.Iterator;

import object.Score;
import object.ScoreDetail;
import object.ScoreList;
import object.ScoreType;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class ScoreListParser {
	
	private ScoreList scores;
	
	public ScoreListParser(){
		
	}
	
	public ScoreListParser(ScoreList scores){
		this.scores=scores;
	}
	
	public void setScoreList(ScoreList scores){
		this.scores=scores;
	}
	
	public Element parse(Document doc){
		if(scores==null||doc==null){
			System.err.println("score or doc cannot be null!");
			return  null;
		}
		
		Element scoreListE=doc.createElement("课程成绩列表");
		
		Iterator<Score> itor=scores.iterator();
		while(itor.hasNext()){
			Element scoreE=doc.createElement("课程成绩");
			scoreListE.appendChild(scoreE);
			
			Score score=itor.next();
			
			String cid=score.getCid();
			scoreE.setAttribute("课程编号", cid);
			
			ScoreType type=score.getType();
			scoreE.setAttribute("成绩性质", type.toString());
			
			Iterator<ScoreDetail> details=score.iterator();
			while(details.hasNext()){
				ScoreDetail detail=details.next();
				
				Element detailE=doc.createElement("成绩");
				scoreE.appendChild(detailE);
				
				String sid=detail.getSid();
				Element sidE=doc.createElement("学号");
				detailE.appendChild(sidE);
				Text sidT=doc.createTextNode(sid);
				sidE.appendChild(sidT);
				
				int grade=detail.getGrade();
				Element gradeE=doc.createElement("得分");
				detailE.appendChild(gradeE);
				Text gradeT=doc.createTextNode(String.valueOf(grade));
				gradeE.appendChild(gradeT);
			}
			
		}
		return scoreListE;
		
	}
	
	

}
